<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cookies
 *
 * @author ZangGuoPing
 */
class Cookies extends CApplicationComponent {

    public $domain;
    public $expire;
    public $path;
    public $key;
    /**
     *
     * @var ZEncrypt 
     */
    private $_encrypt;

    public function init() {
        parent::init();
        $key = Config::model()->getConfig('key');
        $this->_encrypt = new ZEncrypt($key);
    }

    public function set($name, $value, $expire='') {
        $cookie = new CHttpCookie($name, $this->_encrypt->encode($value));
        $cookie->domain = $this->domain ? $this->domain : Yii::app()->request->getServerName();
        $cookie->expire = is_numeric($expire) ? $expire : $this->expire;
        $cookie->path = $this->path ? $this->path : Yii::app()->baseUrl;
        Yii::app()->request->cookies[$name] = $cookie;
    }

    public function get($name) {
        $cookie = Yii::app()->request->getCookies();
        
        $value = $cookie[$name]->value;
        if ($value) {
            return $this->_encrypt->decode($value);
        } else {
            return NULL;
        }
    }

    public function remove($name) {
        $cookie = Yii::app()->request->getCookies();
        unset($cookie[$name]);
    }

    public function clean() {
        $cookie = Yii::app()->request->getCookies();
        unset($cookie);
    }
    public function __set($name, $value){
        $this->set($name, $value);
    }
    public function __get($name){
        return $this->get($name);
    }

}

/**
 * 字符串加密类
 * 通过私钥将字符串加密为一串base64编码字符.并且可以反解密。依赖于php扩展mcrypt
 * @author Administrator
 *
 */
class ZEncrypt {

    private $mAuthkey;

    public function __construct($key='24498936@qq.com') {
        $this->mAuthkey = $key;
    }

    public function encode($value) {
        if (function_exists('mcrypt_module_open')) {
            $td = mcrypt_module_open('tripledes', '', 'ecb', '');
            $td_size = mcrypt_enc_get_iv_size($td);
            $iv = mcrypt_create_iv($td_size, MCRYPT_RAND);
            $key = substr($this->mAuthkey, 0, $td_size);
            mcrypt_generic_init($td, $key, $iv);
            $ret = base64_encode(mcrypt_generic($td, $value));
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $ret;
        } else {
            return base64_encode($value);
        }
    }

    public function decode($value) {
        if (function_exists('mcrypt_module_open')) {
            $td = mcrypt_module_open('tripledes', '', 'ecb', '');
            $td_size = mcrypt_enc_get_iv_size($td);
            $iv = mcrypt_create_iv($td_size, MCRYPT_RAND);
            $key = substr($this->mAuthkey, 0, $td_size);
            mcrypt_generic_init($td, $key, $iv);
            $ret = trim(mdecrypt_generic($td, base64_decode($value)));
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $ret;
        } else {
            return base64_decode($value);
        }
    }

}